package com.br.fsm.projectdelivery.repositorio;

import java.util.ArrayList;
import android.database.Cursor;
import android.util.Log;
import com.br.fsm.projectdelivery.activity.R;
import com.br.fsm.projectdelivery.basica.SugestaoDia;
import com.br.fsm.projectdelivery.basica.SugestaoDia.Sugestao;
import com.br.fsm.projectdelivery.excecao.RepositorioException;
import com.br.fsm.projectdelivery.util.Constantes;

public class RepositorioSugestaoDia extends RepositorioBasico implements IRepositorioSugestaoDia {

	private static RepositorioSugestaoDia instance;	
	
	public static final String TABLE_NAME = "SUGESTAODIA";
	
	public static RepositorioSugestaoDia getInstance(){
		if ( instance == null ){
			instance =  new RepositorioSugestaoDia();
		} 
		return instance;
	}

	@Override
	public SugestaoDia getSugestaoDiaById(int id) throws RepositorioException {
		Cursor cursor = null;
		
		SugestaoDia sugestaoDia = null;
		
		try {
			cursor = db.query(TABLE_NAME, SugestaoDia.getcolumns(), Sugestao.ID + " = " + id , null,
					null, null, null, null);
			
			if (cursor.moveToFirst()) {
				
				sugestaoDia = new SugestaoDia();
				
				int idxId = cursor.getColumnIndex(Sugestao.ID);
				int idxDescricao = cursor.getColumnIndex(Sugestao.DESCRICAO);
				int idxDiaSemana = cursor.getColumnIndex(Sugestao.DIASEMANA);
						
								
				sugestaoDia.setSugestaoId(cursor.getInt(idxId));
				sugestaoDia.setSugestaoDescricao(cursor.getString(idxDescricao));
				sugestaoDia.setSugestaoDiaSemana(cursor.getInt(idxDiaSemana));
				
				}
		} catch (Exception e) {
			e.printStackTrace();
			Log.e( Constantes.CATEGORIA , e.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return sugestaoDia;
	}

	@Override
	public ArrayList<SugestaoDia> getAllSugestaoDia() throws RepositorioException {
		Cursor cursor = null;
		
		ArrayList<SugestaoDia> sugestoesDia = null;
		try {
			sugestoesDia = new ArrayList<SugestaoDia>();
			cursor = db.query(TABLE_NAME, SugestaoDia.getcolumns(), null , null,
					null, null, null, null);
			
			if (cursor.moveToFirst()) {
				
				int idxId = cursor.getColumnIndex(Sugestao.ID);
				int idxDescricao = cursor.getColumnIndex(Sugestao.DESCRICAO);
				int idxDiaSemana = cursor.getColumnIndex(Sugestao.DIASEMANA);
				
				do {
					SugestaoDia sugestaoDia = new SugestaoDia();
					
					sugestaoDia.setSugestaoId(cursor.getInt(idxId));
					sugestaoDia.setSugestaoDescricao(cursor.getString(idxDescricao));
					sugestaoDia.setSugestaoDiaSemana(cursor.getInt(idxDiaSemana));
					
					sugestoesDia.add(sugestaoDia);
				} while (cursor.moveToNext());
				
			}
		} catch (Exception e) {
			e.printStackTrace();			
			Log.e( Constantes.CATEGORIA, e.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return sugestoesDia;
	}

	@Override
	public void insertSugestaoDia(SugestaoDia sugestaoDia) throws RepositorioException {
		/*ContentValues values = new ContentValues();

		values.put(SugestaoDias.ID, promocoes.getPromocoesId());
		values.put(SugestaoDias.DESCRICAO, promocoes.getPromocoesDescricao());
		values.put(SugestaoDias.DIASEMANA, promocoes.getPromocoesDiaSemana());
		
		
		try {
			db.insert(TABLE_NAME, "", values);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}		*/
	}

	@Override
	public void updateSugestaoDia(SugestaoDia sugestaoDia) throws RepositorioException {
		/*ContentValues values = new ContentValues();

		values.put(SugestaoDias.ID, promocoes.getPromocoesId());
		values.put(SugestaoDias.DESCRICAO, promocoes.getPromocoesDescricao());
		values.put(SugestaoDias.DIASEMANA, promocoes.getPromocoesDiaSemana());
		
		String _id = String.valueOf(SugestaoDia.getSugestaoDiaId());

		String where = SugestaoDias.ID + "=?";
		String[] whereArgs = new String[] { _id };

		try {
			db.update(TABLE_NAME, values, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}*/
	}

	@Override
	public void removeSugestaoDia(SugestaoDia sugestaoDia) throws RepositorioException {
		/*String _id = String.valueOf(SugestaoDia.getSugestaoDiaId());

		String where = SugestaoDias.ID + "=?";
		String[] whereArgs = new String[] { _id };

		try {
			db.delete( TABLE_NAME, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}*/
	}
}
